﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmDmNhomkt.
	/// </summary>
	public class frmDmNhomnguon : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.CheckedListBox ckNguon;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butBoqua;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Button butHuy;
		private System.Windows.Forms.Button butSua;
		private System.Windows.Forms.Button butThem;
		private System.Windows.Forms.TextBox txtNoidung;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label ten;
		private int i_nhomkho=0;
		private DataSet ds=new DataSet();
		private DataSet dsNguon=new DataSet();
		private LibKetoan.AccessData d;
		private string m_userroot="";
		private System.Windows.Forms.NumericUpDown txtStt;
		private long l_id=0;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmDmNhomnguon(LibKetoan.AccessData d_acc, int _inhomkho)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			//
			i_nhomkho=_inhomkho;
			d=d_acc;
			m_userroot=d.user;
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmDmNhomnguon));
            this.dataGrid1 = new System.Windows.Forms.DataGrid();
            this.panel1 = new System.Windows.Forms.Panel();
            this.ckNguon = new System.Windows.Forms.CheckedListBox();
            this.butLuu = new System.Windows.Forms.Button();
            this.butBoqua = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.butHuy = new System.Windows.Forms.Button();
            this.butSua = new System.Windows.Forms.Button();
            this.butThem = new System.Windows.Forms.Button();
            this.txtNoidung = new System.Windows.Forms.TextBox();
            this.txtStt = new System.Windows.Forms.NumericUpDown();
            this.label1 = new System.Windows.Forms.Label();
            this.ten = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.txtStt)).BeginInit();
            this.SuspendLayout();
            // 
            // dataGrid1
            // 
            this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
            this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.CaptionVisible = false;
            this.dataGrid1.DataMember = "";
            this.dataGrid1.FlatMode = true;
            this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
            this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
            this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
            this.dataGrid1.Location = new System.Drawing.Point(0, 0);
            this.dataGrid1.Name = "dataGrid1";
            this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
            this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
            this.dataGrid1.ReadOnly = true;
            this.dataGrid1.RowHeaderWidth = 10;
            this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
            this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
            this.dataGrid1.Size = new System.Drawing.Size(448, 349);
            this.dataGrid1.TabIndex = 9;
            this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.ckNguon);
            this.panel1.Controls.Add(this.dataGrid1);
            this.panel1.Location = new System.Drawing.Point(4, 4);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(588, 353);
            this.panel1.TabIndex = 10;
            // 
            // ckNguon
            // 
            this.ckNguon.BackColor = System.Drawing.SystemColors.HighlightText;
            this.ckNguon.CheckOnClick = true;
            this.ckNguon.Location = new System.Drawing.Point(448, 0);
            this.ckNguon.Name = "ckNguon";
            this.ckNguon.Size = new System.Drawing.Size(136, 349);
            this.ckNguon.TabIndex = 10;
            // 
            // butLuu
            // 
            this.butLuu.Image = ((System.Drawing.Image)(resources.GetObject("butLuu.Image")));
            this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butLuu.Location = new System.Drawing.Point(153, 392);
            this.butLuu.Name = "butLuu";
            this.butLuu.Size = new System.Drawing.Size(65, 27);
            this.butLuu.TabIndex = 26;
            this.butLuu.Text = "    &Lưu";
            this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
            // 
            // butBoqua
            // 
            this.butBoqua.Image = ((System.Drawing.Image)(resources.GetObject("butBoqua.Image")));
            this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butBoqua.Location = new System.Drawing.Point(287, 392);
            this.butBoqua.Name = "butBoqua";
            this.butBoqua.Size = new System.Drawing.Size(75, 27);
            this.butBoqua.TabIndex = 28;
            this.butBoqua.Text = "    &Bỏ qua";
            this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(431, 392);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(75, 27);
            this.butKetthuc.TabIndex = 30;
            this.butKetthuc.Text = "    &Kết thúc";
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // butHuy
            // 
            this.butHuy.Image = ((System.Drawing.Image)(resources.GetObject("butHuy.Image")));
            this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butHuy.Location = new System.Drawing.Point(364, 392);
            this.butHuy.Name = "butHuy";
            this.butHuy.Size = new System.Drawing.Size(65, 27);
            this.butHuy.TabIndex = 29;
            this.butHuy.Text = "    &Hủy";
            this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
            // 
            // butSua
            // 
            this.butSua.Image = ((System.Drawing.Image)(resources.GetObject("butSua.Image")));
            this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butSua.Location = new System.Drawing.Point(220, 392);
            this.butSua.Name = "butSua";
            this.butSua.Size = new System.Drawing.Size(65, 27);
            this.butSua.TabIndex = 27;
            this.butSua.Text = "    &Sửa";
            this.butSua.Click += new System.EventHandler(this.butSua_Click);
            // 
            // butThem
            // 
            this.butThem.Image = ((System.Drawing.Image)(resources.GetObject("butThem.Image")));
            this.butThem.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butThem.Location = new System.Drawing.Point(86, 392);
            this.butThem.Name = "butThem";
            this.butThem.Size = new System.Drawing.Size(65, 27);
            this.butThem.TabIndex = 25;
            this.butThem.Text = "    &Thêm";
            this.butThem.Click += new System.EventHandler(this.butThem_Click);
            // 
            // txtNoidung
            // 
            this.txtNoidung.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txtNoidung.Location = new System.Drawing.Point(138, 360);
            this.txtNoidung.Name = "txtNoidung";
            this.txtNoidung.Size = new System.Drawing.Size(449, 20);
            this.txtNoidung.TabIndex = 32;
            // 
            // txtStt
            // 
            this.txtStt.BackColor = System.Drawing.SystemColors.HighlightText;
            this.txtStt.Enabled = false;
            this.txtStt.Location = new System.Drawing.Point(34, 360);
            this.txtStt.Maximum = new decimal(new int[] {
            99,
            0,
            0,
            0});
            this.txtStt.Name = "txtStt";
            this.txtStt.Size = new System.Drawing.Size(40, 20);
            this.txtStt.TabIndex = 31;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(0, 360);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(32, 23);
            this.label1.TabIndex = 33;
            this.label1.Text = "Stt : ";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // ten
            // 
            this.ten.Location = new System.Drawing.Point(75, 360);
            this.ten.Name = "ten";
            this.ten.Size = new System.Drawing.Size(59, 23);
            this.ten.TabIndex = 34;
            this.ten.Text = "Nội dung :";
            this.ten.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // frmDmNhomnguon
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(592, 433);
            this.Controls.Add(this.txtNoidung);
            this.Controls.Add(this.txtStt);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.ten);
            this.Controls.Add(this.butLuu);
            this.Controls.Add(this.butBoqua);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.butHuy);
            this.Controls.Add(this.butSua);
            this.Controls.Add(this.butThem);
            this.Controls.Add(this.panel1);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "frmDmNhomnguon";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Danh mục nhóm nguồn";
            this.Load += new System.EventHandler(this.frmDmNhomnguon_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
            this.panel1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.txtStt)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmDmNhomnguon_Load(object sender, System.EventArgs e)
		{
			f_Load_DmNguon();
			f_Load_DataGrid();
			AddGridTableStyle();
			Enable_Object(false);
			Enable_Button(true);
			butThem.Focus();
		}

		private void f_Load_DmNguon()
		{
			dsNguon=d.get_data("select * from "+m_userroot+".d_dmnguon where nhom="+i_nhomkho.ToString()+" order by stt");
			foreach(DataRow r in dsNguon.Tables[0].Rows) ckNguon.Items.Add(r["ten"].ToString());
		}

		private void f_Load_DataGrid()
		{
			ds=d.get_data("select distinct id,stt,ten from "+m_userroot+".bc_nhomnguon where nhom="+i_nhomkho.ToString()+"");
			dataGrid1.DataSource=ds.Tables[0];
		}

		private void AddGridTableStyle()
		{
			dataGrid1.TableStyles.Clear();
			DataGridTableStyle ts =new DataGridTableStyle();
			ts.MappingName = ds.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=10;
						
			//0
			DataGridTextBoxColumn TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "id";
			TextCol1.HeaderText = "";
			TextCol1.Width = 0;
			ts.GridColumnStyles.Add(TextCol1);
			dataGrid1.TableStyles.Add(ts);		
			//1
			TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "stt";
			TextCol1.HeaderText = "STT";
			TextCol1.Width = 50;
			ts.GridColumnStyles.Add(TextCol1);
			dataGrid1.TableStyles.Add(ts);	
			//2
			TextCol1=new DataGridTextBoxColumn();
			TextCol1.MappingName = "ten";
			TextCol1.HeaderText = "Tên";
			TextCol1.Width =dataGrid1.Width-50;
			ts.GridColumnStyles.Add(TextCol1);
			dataGrid1.TableStyles.Add(ts);	
		}

		private void Clear_Text()
		{
			l_id=0;
			txtNoidung.Text="";
			for(int i=0;i<ckNguon.Items.Count;i++) ckNguon.SetItemChecked(i,false);
		}

		private void Enable_Object(bool ena)
		{
			txtStt.Enabled=ena;
			txtNoidung.Enabled=ena;
			ckNguon.Enabled=ena;
		}

		private void butThem_Click(object sender, System.EventArgs e)
		{
			Clear_Text();
			try
			{
				txtStt.Value=decimal.Parse(d.get_data("select max(nvl(stt,0))+1 from "+m_userroot+".bc_nhomnguon where nhom="+i_nhomkho.ToString()+"").Tables[0].Rows[0][0].ToString());
			}
			catch{txtStt.Value=1;}
			Enable_Button(false);
			Enable_Object(true);
			txtStt.Focus();
		}

		private void Enable_Button(bool ena)
		{
			butThem.Enabled=ena;
			butLuu.Enabled=!ena;
			butBoqua.Enabled=!ena;
			butSua.Enabled=ena;
			butHuy.Enabled=ena;
			butKetthuc.Enabled=ena;
		}

		private bool Kiemtra()
		{
			if(txtNoidung.Text=="")
			{
				MessageBox.Show("Nhập nội dung !",d.Msg);
				txtNoidung.Focus();
				return false;
			}
			for(int i=0;i<ckNguon.Items.Count;i++)
			{
				if(ckNguon.GetItemChecked(i))
				{
					string _asql="select * from "+m_userroot+".bc_nhomnguon where idnguon="+dsNguon.Tables[0].Rows[i]["id"].ToString()+"";
					_asql+=(l_id>0)?" and id<>"+l_id.ToString()+"":"";
					if(d.get_data(_asql).Tables[0].Rows.Count>0)
					{
						return false;
						break;
					}
				}
			}
			return true;
		}

		private void butLuu_Click(object sender, System.EventArgs e)
		{
			if(!Kiemtra()) return ;
			if(l_id==0) l_id=long.Parse(d.get_data("select nvl(max(id),0)+1 as id from "+m_userroot+".bc_nhomnguon").Tables[0].Rows[0][0].ToString());
			d.execute_data("delete from "+m_userroot+".bc_nhomnguon where id="+l_id.ToString()+"");
			for(int i=0;i<ckNguon.Items.Count;i++)
			{
				if(ckNguon.GetItemChecked(i))
				{
					if(!d.upd_bc_nhomnguon(l_id,(long)txtStt.Value,txtNoidung.Text,(long)i_nhomkho,long.Parse(dsNguon.Tables[0].Rows[i]["id"].ToString())))
					{
						MessageBox.Show("Không cập nhật thành công !",d.Msg);
						butBoqua.Focus();
						return;
					}
				}
			}
			f_Load_DataGrid();
			Enable_Button(true);
			Enable_Object(false);
			butThem.Focus();
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
			Enable_Button(false);
			Enable_Object(true);
			txtNoidung.Focus();
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			Enable_Button(true);
			Enable_Object(false);
			butKetthuc.Focus();
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			if(l_id==0)
			{
				MessageBox.Show("Chọn nội dung cần hủy !",d.Msg);
				dataGrid1.Focus();
				return;
			}
			if(MessageBox.Show("Hủy nội dung khai báo này !",d.Msg,MessageBoxButtons.YesNo)==DialogResult.Yes)
			{
				d.execute_data("delete from "+m_userroot+".bc_nhomnguon where id="+l_id.ToString()+"");
				f_Load_DataGrid();
			}

		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			System.GC.Collect();
			this.Close();
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
				int i=dataGrid1.CurrentRowIndex;
				CurrencyManager cm=(CurrencyManager)BindingContext[dataGrid1.DataSource,dataGrid1.DataMember];
				DataView dv=(DataView)cm.List;
				dv.AllowNew=false;
				dv.AllowEdit=false;
				l_id=long.Parse(dv[i]["id"].ToString());
				txtStt.Value= decimal.Parse(dv[i]["stt"].ToString());
				txtNoidung.Text=dv[i]["ten"].ToString();
				for(int i1=0;i1<ckNguon.Items.Count;i1++) ckNguon.SetItemChecked(i1,false);
				foreach(DataRow r in d.get_data("select * from "+m_userroot+".bc_nhomnguon where id="+l_id.ToString()+"").Tables[0].Rows)
				{
					for(int i1=0;i1<ckNguon.Items.Count;i1++)
					{
						if(dsNguon.Tables[0].Rows[i1]["id"].ToString()==r["idnguon"].ToString()) ckNguon.SetItemChecked(i1,true);
					}
				}
			}
			catch{}
		}
	}
}
